package com.joysuch.wwyt.openapi.services.serviceV1.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zlg.common.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.bp.entity.BpWorkType;
import com.joysuch.wwyt.bp.entity.dsl.QBpJob;
import com.joysuch.wwyt.bp.entity.dsl.QBpWorkType;
import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.bp.repository.BpWorkTypeDao;
import com.joysuch.wwyt.core.common.util.DateTimeUtil;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.dzhapi.bean.ApiBpJobBean;
import com.joysuch.wwyt.dzhapi.bean.ApiBpWorkTypeBean;
import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.mapper.ApiBpWorkTypeMapper;
import com.joysuch.wwyt.mh.DateUtils;
import com.joysuch.wwyt.openapi.constants.ApiCode;
import com.joysuch.wwyt.openapi.constants.ApiStatus;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1QueryListReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiPagePropertyDataVo;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiV1BpJobSearchVo;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiV1BpWorkTypeVo;
import com.joysuch.wwyt.openapi.exception.ApiV1ExceptionCodeMsg;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1BpWorkService;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.util.StringUtil;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.Optional;

/**
 * @author: JamesZY
 * @date: 2021/8/24
 * @description:
 */
@Slf4j
@Service
public class ApiV1BpWorkServiceImpl implements ApiV1BpWorkService {
    @Autowired
    private BpWorkTypeDao dao;
    @Autowired
    private ApiBpWorkTypeMapper workTypeMapper;

    @Override
    public ApiPagePropertyDataVo searchAll(ApiV1QueryListReq req, BuildDataDto dto) throws ApiV1ExceptionCodeMsg {
        QueryWrapper<ApiBpWorkTypeBean> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(req.getStartUpdateTime())) {
            queryWrapper.ge("UPDATE_TIME", req.getStartUpdateTime());
        }
        if (ObjectUtil.isNotEmpty(req.getEndUpdateTime())) {
            queryWrapper.le("UPDATE_TIME", req.getEndUpdateTime());
        }
        if (StringUtil.notEmpty(req.getUniqueId())) {
            queryWrapper.eq("unique_id", req.getUniqueId());
        }
        queryWrapper.eq("TENENT_ID", dto.getTenentId());
        queryWrapper.eq("ORG_CODE", dto.getOrgCode());
        queryWrapper.eq("DELETE_FLAG", "0");

        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ApiBpWorkTypeBean> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(req.getPageNumber(), req.getPageSize());
        workTypeMapper.selectPage(page, queryWrapper);
        List<ApiV1BpWorkTypeVo> list = Lists.newArrayList();
        if (CollectionUtils.isEmpty(page.getRecords())) {
            for (ApiBpWorkTypeBean workType : page.getRecords()) {
                ApiV1BpWorkTypeVo vo = new ApiV1BpWorkTypeVo();
                vo.setUniqueId(workType.getUniqueId());
                vo.setName(workType.getName());
                vo.setCreateTime(workType.getCreateTime());
                vo.setUpdateTime(workType.getUpdateTime());
                list.add(vo);
            }
        }
        ApiPagePropertyDataVo apiPagePropertyDataVo = new ApiPagePropertyDataVo();
        apiPagePropertyDataVo.setData(page.getRecords());
        apiPagePropertyDataVo.setTotalElements(page.getTotal());
        apiPagePropertyDataVo.setTotalPages((int) page.getPages());
        apiPagePropertyDataVo.setPresentPage((int) page.getCurrent());
        return apiPagePropertyDataVo;
    }
}
